
/* Variables representing parameter values in the READ-JSON( )
   and WRITE-JSON( ) methods. */

   DEFINE VARIABLE vc_SourceType             AS CHARACTER NO-UNDO.
   DEFINE VARIABLE vc_TargetType             AS CHARACTER NO-UNDO.
   DEFINE VARIABLE vc_File                   AS CHARACTER NO-UNDO.
   DEFINE VARIABLE vc_ReadMode               AS CHARACTER NO-UNDO.
   DEFINE VARIABLE vc_Encoding               AS CHARACTER NO-UNDO.
   DEFINE VARIABLE vl_Formatted              AS LOGICAL   NO-UNDO.
   DEFINE VARIABLE vc_errorstring            AS CHARACTER NO-UNDO.
   DEFINE VARIABLE vc_writejsonfile          AS CHARACTER NO-UNDO.
   define variable hresult                   as handle no-undo.
   
   DEFINE VARIABLE hdataset       AS HANDLE    NO-UNDO.
   DEFINE VARIABLE vl_RetOK       AS LOGICAL   NO-UNDO.
   DEFINE VARIABLE vl_wtOK       AS LOGICAL   NO-UNDO.


   define {1} shared temp-table api_status no-undo
               field data_seq           as integer
               field traceid            as character
               field domain            as character
               field dataid             as character
               field billnbr            as character
               field return_status      as character
               field message_type       as character
               field message_string     as character.

   
   
   hresult = temp-table api_status:handle.

procedure loadjsonfile:   

   define input parameter ic_inputjson as character no-undo.
   define input parameter ic_outputjson as character no-undo.
   define output parameter ol_status as logical no-undo.

   
   ic_outputjson = replace(ic_inputjson,".json",".run.out.json").
  
   put unformatted ic_outputjson skip.
   run readjsonfile(input hdataset,
                    input ic_inputjson,
                    input "file",
                    input "EMPTY",
                    output vl_RetOK,
                    output vc_errorstring).
   
                    // message vc_errorstring view-as alert-box.            
      

   /* When read json file failed  */   
   if not vl_RetOK then do:
     create api_status.	
     assign api_status.traceid = replace(ic_inputjson,".json","")
            api_status.message_string = gettermlabel("DOCUMENT_NUMBER",4) 
                    + gettermlabel("Read",4)
                    + gettermlabel("ERROR",4)
                    + "_"
                    + vc_errorstring
            api_status.message_type  = "JSON_DATA_ERROR"
            api_status.return_status   = "Failed"
            api_status.domain = global_domain
            api_status.data_seq = 1.
    
    run writejsonfile(input hresult,
                      input ic_outputjson,
                      input "FILE",
                      input True,
                      input "UTF-8",
                      output vl_wtOK,
                      output vc_errorstring).
   
  end.
  
  ol_status = vl_RetOK.
 
end procedure.

procedure writejsonfile:
   
   define input parameter  hwjsonfile  as handle    no-undo.
   define input parameter  ctagetfile as character no-undo.
   define input parameter  ctagettype as character no-undo.
   define input parameter  lformatted as logical   no-undo.
   define input parameter  cEncoding  as character no-undo.
   define output parameter lReturnValue as logical no-undo.
   define output parameter cerrormsg    as character no-undo.
   
   do on error undo, leave:
   lReturnValue = hwjsonfile:WRITE-JSON(ctagettype, ctagetfile, lFormatted, cEncoding, no , no).
   catch SyError as Progress.Lang.SysError:
   cerrormsg = SyError:GetMessage(1).
   end catch.
   end.
end.

procedure readjsonfile:
  
   define input parameter  hrjsonfile   as handle      no-undo.
   define input parameter  creadfile    as character   no-undo.
   define input parameter  cSourceType  as character   no-undo.
   define input parameter  cReadMode    as character   no-undo.
   define output parameter lReturnValue as logical     no-undo.
   define output parameter cerrormsg    as character   no-undo.

  do on error undo, leave:
   lReturnValue = hrjsonfile:READ-JSON(cSourceType, creadfile, cReadMode). 
   catch SyError as Progress.Lang.SysError:
   cerrormsg =  SyError:GetMessage(1) .
   end catch.

  end.

end.

